package jp.co.dailyReport.weeklyReport;

import java.util.Calendar;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportWorkItemInsertExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportWorkItemWIIdLoadExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportWorkItemDeleteExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportWorkItemLoadExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportWorkItemTotalWorkingHoursExecuter;
import jp.co.dailyReport.weeklyReport.WeeklyReportWorkItem;
import jp.co.dailyReport.workItem.WorkItemManager;

/**
 * 週報作業項目情報管理クラス
 */
public class WeeklyReportWorkItemManager {

	private static Logger log = Logger.getLogger(WeeklyReportManager.class);

	/**
	 * 週報作業項目情報を取得します。
	 * 
	 * @param weeklyReportId 週報ID
	 * @return 週報作業項目オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static WeeklyReportWorkItem[] load(int weeklyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportWorkItemLoadExecuter weeklyReportWorkItemLoadExecuter = new WeeklyReportWorkItemLoadExecuter(weeklyReportId);
		Object objs[] = null;

		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportWorkItemLoadExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs == null || objs.length == 0) {
			return new WeeklyReportWorkItem[0];
		} else {
			return (WeeklyReportWorkItem[])objs;
		}
	}

	/**
	 * 週報作業項目情報を取得します。
	 * 
	 * @param projectId プロジェクトID
	 * @param workDateStart 指定開始作業日
	 * @param workDateEnd 指定終了作業日
	 * @return 週報作業項目オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static WeeklyReportWorkItem[] load(int projectId,Calendar workDateStart,Calendar workDateEnd) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportWorkItemTotalWorkingHoursExecuter weeklyReportWorkItemTotalWorkingHoursExecuter = new WeeklyReportWorkItemTotalWorkingHoursExecuter(projectId,workDateStart,workDateEnd);
		Object objs[] = null;
		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportWorkItemTotalWorkingHoursExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs == null || objs.length == 0) {
			return new WeeklyReportWorkItem[0];
		} else {
			return (WeeklyReportWorkItem[])objs;
		}
	}


	/**
	 * 週報作業項目情報を取得します。
	 * 
	 * @param workItemId 作業項目ID
	 * @return 週報作業項目オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static WeeklyReportWorkItem[] loadWIId(int workItemId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportWorkItemWIIdLoadExecuter weeklyReportWorkItemWIIdLoadExecuter = new WeeklyReportWorkItemWIIdLoadExecuter(workItemId);
		Object objs[] = null;

		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportWorkItemWIIdLoadExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs == null || objs.length == 0) {
			return new WeeklyReportWorkItem[0];
		} else {
			return (WeeklyReportWorkItem[])objs;
		}
	}

	/**
	 * 週報作業項目情報を登録します。
	 * 
	 * @param weeklyReportWorkItem 週報作業項目オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void insert(WeeklyReportWorkItem weeklyReportWorkItem) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportWorkItemInsertExecuter weeklyReportWorkItemInsertExecuter = new WeeklyReportWorkItemInsertExecuter(weeklyReportWorkItem);

		try {
			ExecuterManager.execute(connectionFactory, weeklyReportWorkItemInsertExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 週報作業項目情報を削除します。
	 * 
	 * @param weeklyReportId 週報ID
	 * @throws DataUnavailableException
	 */
	public static void delete(int weeklyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportWorkItemDeleteExecuter weeklyReportWorkItemDeleteExecuter = new WeeklyReportWorkItemDeleteExecuter(weeklyReportId);

		try {
			ExecuterManager.execute(connectionFactory, weeklyReportWorkItemDeleteExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 作業時間の合計値を求めます。
	 * 
	 * @param weeklyReportWorkItem 週報作業項目オブジェクトの配列
	 * @return double
	 */
	public static double calculate(WeeklyReportWorkItem[] weeklyReportWorkItem) throws DataUnavailableException {

		double totalWorkingHours = 0.0;
		for(int i = 0; i < weeklyReportWorkItem.length; i++) {
			if(WorkItemManager.load(weeklyReportWorkItem[i].getWorkItemId()).getAddStaffId()==0){
				totalWorkingHours += weeklyReportWorkItem[i].getWorkingHours();
			}
		}
		return totalWorkingHours;
	}

}
